package com.info.zhiduoduo.common.utils;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * Created by Lance on 2017/10/8 0008.
 */
public class ShopUtils {
    public static final DecimalFormat df2 =new DecimalFormat("#.00");
    /**
     * 向指定URL发送GET方法的请求
     *
     * @param url
     *            发送请求的URL
     *            请求参数，请求参数应该是 name1=value1&name2=value2 的形式。
     * @return URL 所代表远程资源的响应结果
     */
    public static String sendGet(String url) {
        String result = "";
        BufferedReader in = null;
        try {
            String urlNameString = url ;
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            HttpURLConnection connection =(HttpURLConnection) realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
            // 定义 BufferedReader输入流来读取URL的响应
            int respCode=connection.getResponseCode();
            if(respCode>=400){
                in = new BufferedReader(new InputStreamReader(
                        connection.getErrorStream()));
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                }
                System.out.println("http ["+url+"] errorcode ["+respCode+"] result:"+result);
            }else {
                in = new BufferedReader(new InputStreamReader(
                        connection.getInputStream()));
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                }
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常！" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }
    public static String postData(String url,String sendData) {
        String responsorUrl = url;

        HttpURLConnection urlConnection = null;
        try {
            URL aURL = new URL(responsorUrl);

            urlConnection = (HttpURLConnection) aURL.openConnection();

            urlConnection.setRequestMethod("POST");
            urlConnection.setDoInput(true);
            urlConnection.setDoOutput(true);
            urlConnection.setUseCaches(false);
            urlConnection.setRequestProperty("User-Agent", "MSIE");
            urlConnection.connect();
            if (sendData != null && sendData.trim().length() != 0) {
                urlConnection.getOutputStream().write(sendData.getBytes("utf-8"));
            }

            int contentLen = urlConnection.getContentLength();

            java.io.DataInputStream in = new java.io.DataInputStream(urlConnection.getInputStream());

            byte buffer[] = new byte[contentLen];

            int len = 0;

            while (len < contentLen) {
                int remainedLen = contentLen - len;
                if (remainedLen > 1024)
                    remainedLen = 1024;
                int readLen = in.read(buffer, len, remainedLen);
                if (readLen == -1 || readLen == 0) {
                    break;
                }

                len = len + readLen;
            }

            urlConnection.disconnect();
            urlConnection = null;
            String a = new String(buffer, 0, contentLen, "utf8");

            return a;

        } catch (Throwable e) {
            e.printStackTrace();
            if (urlConnection != null) {
                try {
                    urlConnection.disconnect();
                    urlConnection = null;
                } catch (Exception ee) {
                    ee.printStackTrace();
                }
            }
        } finally {
            if (urlConnection != null) {
                try {
                    urlConnection.disconnect();
                    urlConnection = null;
                } catch (Exception ee) {
                }
            }
        }
        return "";
    }

    public static String createTradeNo(){
//        String s=DateUtil.getNowDateString(DateUtil.FORMAT_STYLE_7)+generateShortUuid();
        String s1=DateUtil.getNowDateString(DateUtil.FORMAT_STYLE_7)+RandomUtil.getRandomNum(4);
        return s1;
    }

    public static String createCouponNo(String prefix) {
        return prefix + DateUtil.getNowDateString(DateUtil.FORMAT_STYLE_7) + RandomUtil.getRandomNum(4);
    }

    public static String[] chars = new String[] { "a", "b", "c", "d", "e", "f",
            "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
            "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5",
            "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I",
            "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z" };


    public static String generateShortUuid() {
        StringBuffer shortBuffer = new StringBuffer();
        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (int i = 0; i < 8; i++) {
            String str = uuid.substring(i * 4, i * 4 + 4);
            int x = Integer.parseInt(str, 16);
            shortBuffer.append(chars[x % 0x3E]);
        }
        return shortBuffer.toString();

    }

    public static void main(String[] args) {
        System.out.println("isyshop1234567890aijiuai98765432".length());
    }
}
